package edu.mis.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import edu.mis.model.base.BaseOrderList;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
@SuppressWarnings("serial")
public class OrderList extends BaseOrderList<OrderList> {


	public static final OrderList dao = new OrderList().dao();

	private Collection<OrderDetail> orderDetails = new HashSet<>(0);

	private User user = new User();

	private Seat seat = new Seat();

	/**
	 * 表关联查询得到菜品的菜品号
	 * 类型：订单为1：N关系，得到菜品对象的类型
	 * @return
	 */
	public List<Record> findAllOrderDetailsWithCommodity(int id){
		return Db.find("SELECT d.id,d.qty,c.commodityName,c.price FROM(SELECT * FROM order_detail  WHERE orderListId=?) d LEFT JOIN commodity c on d.commodityId=c.id",id);
	}
	/**
	 * 关联查询_查询所有订单及其类型(类型:订单为1对多)
	 * @return
	 */
	public List<Record> findAllRecord(){
		return Db.find("SELECT c.*,s.seatNumber,o.orderType FROM order_list c LEFT JOIN seat s ON c.seatId=s.id LEFT JOIN ordertype o ON c.orderTypeId=o.id " );
	}
	public List<Record> findAllUnpaidRecord(){
		return Db.find("SELECT l.*,s.seatNumber,o.orderType FROM (SELECT * FROM order_list WHERE orderTypeId=1) l LEFT JOIN seat s ON l.seatId = s.id LEFT JOIN ordertype o ON l.orderTypeId = o.id" );
	}
	/**
	 * 查询所有订单
	 * @return
	 */
	public List<OrderList> findAll(){
		return dao.find("SELECT * FROM order_list");
	}
	/**
	 * 根据订单名查找订单
	 * @param orderName
	 * @return
	 */
	public OrderList findOrderlistByOrderName(String orderName){
		return findFirst("SELECT * FROM order_list WHERE orderName = ? ",orderName);
	}
    public OrderList findOrderListByDate(Date orderTime) {
        return findFirst("SELECT * FROM order_list WHERE orderTime = ? ",orderTime);
    }


	public User findUserByUserId(){
		return User.dao.findById(get("userId"));
	}

	/**
	 * 表关联查询_得到订单的类型
	 * 类型:订单为1:N关系，得到订单对象的类型。
	 * @return
	 */
	public Seat findSeatBySeatId(){
		return Seat.dao.findById(get("seatId"));
	}
	public Seat findSeatBySeatId(Integer seatId){
		return Seat.dao.findById(seatId);
	}

     public Ordertype findOrderType(){
		return Ordertype.dao.findById(get("orderType"));
	 }

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.setUserId(user.getId());
		this.user = user;
	}

	public Seat getSeat() {
		return seat;
	}

	public void setSeat(Seat seat) {
		this.setSeatId(seat.getId());
		this.seat = seat;
	}

	public Collection<OrderDetail> getOrderDetails() {
		return orderDetails;
	}

	public void setOrderDetails(Collection<OrderDetail> orderDetails) {
		this.orderDetails = orderDetails;
	}
}
